package ca.ets.osgi.conferenceroomservice;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * Contains a list of rooms
 * @author HP_Administrator
 *
 */
public class ListRooms {
	
	//table containing the room's name and the room itself
	private Hashtable<String,Room> hRoom = new Hashtable<String,Room>();
		
	//instance of the class
	private static ListRooms instance;
	
	//making a singleton class
	private ListRooms(){}

	/**
	 * Return a single instance of the classe
	 * @return
	 */
	public static ListRooms getInstance(){
		if(instance ==null)
			instance = new ListRooms();
		return instance;
	}

	/**
	 * @return the hRoom
	 */
	public Hashtable<String, Room> gethRoom() {
		return hRoom;
	}

	/**
	 * @param hRoom the hRoom to set
	 */
	public void sethRoom(Hashtable<String, Room> hRoom) {
		this.hRoom = hRoom;
	}
	
	/**
	 * return whether a room is indeed in the table
	 * @param roomID
	 * @return
	 */
	public boolean isRoomPresent(String roomID){
		boolean response = false;
		Room room = this.hRoom.get(roomID);
		if(room !=null)
			response = true;
		return response;
	}
	
	
	/**
	 * Add the room to the table
	 * @param roomID
	 * @param room
	 */
	public void addRoomToList(String roomID, Room room){
		if(!this.isRoomPresent(roomID)){
			this.hRoom.put(roomID,room );
			System.out.println("Adding: " + room.toString());
			//System.out.println("");
		}
		
	}
	
	/**
	 * Remove a room from a table
	 * @param roomID
	 */
	public void removeRoomFromList(String roomID){
		if(this.isRoomPresent(roomID)){
			this.hRoom.remove(roomID);
			System.out.println("Room " + roomID + " was added to the table");
		}
		
	}
	
	/**
	 * Returns a room
	 * @param roomID
	 * @return
	 */
	public Room getRoom(String roomID){
		return this.hRoom.get(roomID);
	}
	
	/**
	 * Change the name of the room
	 * @param roomID
	 * @param newName
	 */
	public void changeRoomName(String roomID, String newName){
		if(this.isRoomPresent(roomID)){
			Room room = this.hRoom.get(roomID);
			room.setName(newName);
			System.out.println("The room " + roomID + " was modified with new name to " + newName);
		}

	}
	
	/**
	 * Change the surface of the room
	 * @param roomID
	 * @param newSurface
	 */
	public void changeRoomSurface(String roomID, String newSurface){
		if(this.isRoomPresent(roomID)){
			Room room = this.hRoom.get(roomID);
			room.setSurface(newSurface);
			System.out.println("The room " + roomID + " was modified with new surface to " + newSurface);
		}
    }
	
	/**
	 * Change the availability of a room's projector
	 * @param roomID
	 * @param newProjector
	 */
	public void changeRoomProjectAvailability(String roomID, boolean newProjector){
		if(this.isRoomPresent(roomID)){
			Room room = this.hRoom.get(roomID);
			room.setProjectorAvailable(newProjector);
			System.out.println("The room " + roomID + " was modified with new projector availability to " + newProjector);
		}
    }
	
	
	/**
	 * Return a list of actual rooms or ID rooms
	 * @param choice true for only the rooms ID
	 * @param roomID
	 * @param surface
	 * @param projector
	 * @return
	 */
	public ArrayList<Object> getListRoomsWithCaracteristics(boolean choice,String roomID, String surface, boolean projector){
		ArrayList<Object> list = new ArrayList<Object>();
		Enumeration<Room> emu = this.hRoom.elements();
		while(emu.hasMoreElements()){
			Room room = emu.nextElement();
			if(room.isRoom1EqualtoRoom2(roomID, surface, projector)){
				if(choice ==true){
					list.add(roomID);
					System.out.println("A room matching your description was found and that name was added to the list ");
				}
				
				else if(choice==false){
					list.add(room);
					System.out.println("A room matching your description was found and was added to the list ");
				}
					
			}
		}
		if(list.size()==0){
			list = null;
			System.out.println("No Rooms were found with these description");
		}
		return list;
	}
	
	/**
	 * Return a list of actual rooms or ID rooms
	 * @param choice true for only the rooms ID
	 * @param surface
	 * @param projector
	 * @return
	 */
	public ArrayList<Object> getListRoomsWithCaracteristics(boolean choice,String surface, boolean projector){
		ArrayList<Object> list = new ArrayList<Object>();
		Enumeration<Room> emu = this.hRoom.elements();
		while(emu.hasMoreElements()){
			Room room = emu.nextElement();
			if(room.isRoom1EqualtoRoom2(surface, projector)){
				if(choice ==true){
					list.add(room.getName());
					System.out.println("A room matching your description was found and that name was added to the list ");
					System.out.println(room.toString());
					System.out.println("");
				}
				
				else if(choice==false){
					list.add(room);
					System.out.println("A room matching your description was found and was added to the list ");
					System.out.println(room.toString());
					System.out.println("");
				}
					
			}
		}
		if(list.size()==0){
			list = null;
			System.out.println("No Rooms were found with these description");
		}
		return list;
	}

}
